<?php
session_start(); //一定要先注册会话！！！
if (@$_GET['action'] == "login") {
    $_SESSION['userName'] = $_POST['userName'];
    $_SESSION['password'] = $_POST['password'];
    if ($_SESSION['userName'] == "123" && $_SESSION['password'] == "123") {
        header("Location: homePage.php");
    } else {
        echo "密码错误";
    }
}
if (@$_GET['action'] == "logout") {
    if (isset($_SESSION['userName']) or isset($_SESSION['password'])) {
        $_SESSION[] = array();
        unset($_SESSION['userName']);
        unset($_SESSION['password']);
        if (isset($_COOKIE['userName'])) {
            setcookie('username', '', time() - 3600);
        }
        if (isset($_COOKIE['password'])) {
            setcookie('password', '', time() - 3600);
        }
        session_destroy(); //销毁会话
    }
}
?>
<HTML>
<BODY>
<h1>用户登录</h1>
<form action="Login.php?action=login" method="post">
<th>用户名：</th><td>
<input class="txt" type="text" name="userName" required></td>
<th>密码：</th>
<td>
<input class="txt" type="text" name="password" required></td>
<td><input value="提交" type="submit"></td>
</form>
</BODY>
</HTML>
